package com.cci.kangdao.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cci.kangdao.aop.annotation.NoDuplicateSubmission;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.common.WorkOrderStatusException;
import com.cci.kangdao.service.OrderManageService;
import com.cci.kangdao.utilTool.ParameterTool;

@Controller
public class OrderManageController {

	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Autowired
	private OrderManageService orderManageService;
	
	/**
	 * 400回访
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@NoDuplicateSubmission
	@RequestMapping("/returnVisit.aspx")
	public ModelAndView returnVisit(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return orderManageService.returnVisit(map);
		} catch (WorkOrderStatusException wos) {
			ParameterTool.writeResponse(response, 21, "工单状态已变更，请刷新APP再试");
		} catch (Exception ex) {
			// TODO: handle exception
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 400远程解决
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@NoDuplicateSubmission
	@RequestMapping("/resolveRemoteCCEC.aspx")
	public ModelAndView resolveRemoteCCEC(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return orderManageService.resolveRemoteCCEC(map);
		} catch (WorkOrderStatusException wos) {
			ParameterTool.writeResponse(response, wos.getMessage());
		} catch (Exception ex) {
			// TODO: handle exception
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 400重新指派服务站
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@NoDuplicateSubmission
	@RequestMapping("/reassignServiceStation.aspx")
	public ModelAndView reassignServiceStation(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return orderManageService.reassignServiceStation(map);
		} catch (WorkOrderStatusException wos) {
			ParameterTool.writeResponse(response, wos.getMessage());
		} catch (Exception ex) {
			// TODO: handle exception
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 400主管关闭工单
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@NoDuplicateSubmission
	@RequestMapping("/closeOrderByManager.aspx")
	public ModelAndView closeOrderByManager(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return orderManageService.closeOrderByManager(map);
		} catch (WorkOrderStatusException wos) {
			ParameterTool.writeResponse(response, 21, "工单状态已变更，请刷新APP再试");
		} catch (Exception ex) {
			// TODO: handle exception
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 400主管继续服务
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@NoDuplicateSubmission
	@RequestMapping("/continueService.aspx")
	public ModelAndView continueService(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return orderManageService.continueService(map);
		} catch (WorkOrderStatusException wos) {
			ParameterTool.writeResponse(response, 21, "工单状态已变更，请刷新APP再试");
		} catch (Exception ex) {
			// TODO: handle exception
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 400审核不通过
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@NoDuplicateSubmission
	@RequestMapping("/auditFailed.aspx")
	public ModelAndView auditFailed(HttpServletRequest request, HttpServletResponse response) throws Exception{
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return orderManageService.auditFailed(map);
		} catch (WorkOrderStatusException wos) {
			ParameterTool.writeResponse(response, wos.getMessage());
		} catch (Exception ex) {
			// TODO: handle exception
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
}
